<?php
class Admin extends Controller
{
	
	function __construct()
	{
		parent::Controller();
		$this->quickauth->restrict('admin');
		$this->load->model('admin_model','model');
		$this->load->helper('file');
	}
	
	function Admin()
	{
		self::__construct();
	}
	
	function index()
	{
		$this->load->view('header');
		$this->load->view('upload');
		$this->load->view('footer');
	}
	
	/*upload
	* checks to see if a file has been uploaded, if it has then process
	* it, if not then show an upload form.
	*/
	function upload(){
		$this->load->library('form_validation');
		$this->form_validation->set_rules('file', 'File', 'required');
		
		if (!($this->input->post('submit')))
		{
			$this->load->view('header');
			$this->load->view('upload');
			$this->load->view('footer');
		}
		else
		{
			$name = $this->input->post('name');
			$url = $this->input->post('url');
			$description = $this->input->post('description');
			
			$config['upload_path'] = './static/uploads';
			$config['allowed_types'] = 'zip|jpeg|jpg|gif|html|css|tar.gz|tar|js|htm|rar|png|tif|psd|xcf|svg|ai|gz|deb|rpm|txt|dat|conf|pdf|sql|mp3|wav|mp4|flac|mov|avi|wmv|wma|flv|mpg|mkv';
			$this->load->library('upload', $config);
			
			if ( ! $this->upload->do_upload('file'))
			{
				$error = array('error' => $this->upload->display_errors());
				$this->load->view('header');
				$this->load->view('upload', $error);
				$this->load->view('footer');
			}	
			else
			{
				$data_arr = $this->upload->data();
				$this->model->append_log(
					$_SESSION['username'],
					'Core',
					"Uploaded " . $data_arr['file_name'] . ""
				);
				
				$data['success'] = "File ". site_url('static/uploads') . '/' . $data_arr['file_name'] . " has been uploaded.";
				$this->load->view('header');
				$this->load->view('upload', $data);
				$this->load->view('footer');
			}
			
		}
	}
}
